<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="文章标题" prop="articleTitle">
        <el-input
          v-model="queryParams.articleTitle"
          placeholder="请输入文章标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="文章类型" prop="articleType">
        <el-select
          v-model="queryParams.articleType"
          placeholder="文章类型"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in dict.type.blog_article_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="文章状态"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in dict.type.sys_normal_disable"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="发布时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="['00:00:00', '23:59:59']"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
        >搜索
        </el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
        >重置
        </el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          @click="handleAdd"
          v-hasPermi="['blog:article:add']"
        >发布文章
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['blog:article:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-refresh"
          @click="handleInitEsIndex"
          v-hasPermi="['blog:article:add']"
        >初始化ES索引
        </el-button>
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      border
      stripe
      v-loading="loading"
      :data="articleList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column
        label="文章标题"
        align="center"
        prop="articleTitle"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="文章图片"
        align="center"
        prop="articleImageUrl"
        width="200px"
      >
        <template slot-scope="scope">
          <image-preview
            :src="scope.row.articleImageUrl"
            :width="120"
            :height="60"
          />
        </template>
      </el-table-column>
      <el-table-column label="文章类型" align="center" prop="articleType">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.blog_article_type"
            :value="scope.row.articleType"
          />
        </template>
      </el-table-column>
      <el-table-column label="文章分类" align="center" prop="categoryName">
        <template slot-scope="scope">
          <el-tag type="primary" disable-transitions>{{
              scope.row.categoryName
            }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="置顶" align="center" prop="isTop">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.isTop"
            active-value="1"
            inactive-value="0"
            active-color="#13ce66"
            @change="handleIsTopChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="文章状态" align="center" key="status">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            active-value="0"
            inactive-value="1"
            @change="handleStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column
        label="点赞量"
        align="center"
        prop="likedCount"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="浏览量"
        align="center"
        prop="viewedCount"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="发布时间" align="center" prop="createTime"/>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['blog:article:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['blog:article:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import {
  listArticle,
  delArticle,
  changeArticleStatus,
  changeArticleIsTop, initEsIndex
} from '@/api/blog/article'
import MiddleUtil from '@/utils/middleUtil'

export default {
  name: 'Article',
  dicts: ['sys_normal_disable', 'blog_article_type'],
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 博客文章表格数据
      articleList: [],
      dateRange: [],
      // 弹出层标题
      title: '',
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        articleTitle: undefined,
        articleType: undefined,
        status: undefined
      }
    }
  },
  created() {
    this.getList()
  },
  mounted() {
    let that = this
    MiddleUtil.$on('getList', () => {
      that.getList()
    })
  },
  methods: {
    /** 查询博客文章列表 */
    getList() {
      this.loading = true
      listArticle(this.addDateRange(this.queryParams, this.dateRange)).then(
        (response) => {
          this.articleList = response.rows
          this.total = response.total
          this.loading = false
        }
      )
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = []
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.articleId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$router.push(`/blog/article-operate`)
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$router.push(`/blog/article-operate/${row.articleId}`)
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const articleIds = row.articleId || this.ids
      this.$modal
        .confirm('是否确认删除博客文章编号为"' + articleIds + '"的数据项？')
        .then(() => {
          this.loading = true
          return delArticle(articleIds)
        })
        .then(() => {
          this.loading = false
          this.getList()
          this.$modal.msgSuccess('删除成功')
        })
        .catch(() => {
        })
        .finally(() => {
          this.loading = false
        })
    },
    //状态修改
    handleStatusChange(row) {
      let text = row.status === '0' ? '启用' : '停用'
      this.$modal
        .confirm(`确认要'${text}' 此文章吗？`)
        .then(function() {
          return changeArticleStatus(row.articleId, row.status)
        })
        .then(() => {
          this.$modal.msgSuccess(text + '成功')
        })
        .catch(function() {
          row.status = row.status === '0' ? '1' : '0'
        })
    },
    //置顶修改
    handleIsTopChange(row) {
      let text = row.isTop === '0' ? '取消置顶' : '置顶'
      this.$modal
        .confirm(`确认要'${text}' 此文章吗？`)
        .then(function() {
          return changeArticleIsTop(row.articleId, row.isTop)
        })
        .then(() => {
          this.$modal.msgSuccess(text + '成功')
        })
        .catch(function() {
          row.isTop = row.isTop === '0' ? '1' : '0'
        })
    },
    handleInitEsIndex() {
      this.$modal
        .confirm('是否创建文章ES索引，并全量同步至ES？')
        .then(() => {
          this.$modal.loading("正在创建索引并同步数据，请稍后")
          return initEsIndex()
        })
        .then(() => {
          this.$modal.closeLoading();
          this.$modal.msgSuccess('索引已创建，数据同步成功！')
        })
        .catch(() => {
        })
        .finally(() => {
          this.$modal.closeLoading();
        })
    },
  }
}
</script>
